<template>
  <div class="container1" :class="{ print: type == '打印' }">
    <DocHeader fromDoc="surgicalHandover" :title="title" :bottomBorder="false">
    </DocHeader>
    <Form1 ref="form1Ref" :form="pageData.form1" />
    <Form2 ref="form2Ref" :form="pageData.form2" />
  </div>
</template>
<script>
import { cloneDeep } from 'lodash-unified'
import {
  computed,
  defineComponent,
  nextTick,
  onMounted,
  reactive,
  toRefs,
  watch
} from 'vue'

import DateSelection from '@/components/DateSelection/indexV2.vue'
import { getStoreFromInstance } from '@/utils/instance'

import DocHeader from '../DocHeader.vue'
import { baseData } from './baseData.js'
import Form1 from './form1.vue'
import Form2 from './form2.vue'

export default defineComponent({
  components: {
    DocHeader,
    DateSelection,
    Form1,
    Form2
  },
  props: {
    data: {
      type: Object,
      default: () => ({})
    },
    title: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    }
  },
  setup(props) {
    const state = reactive({
      pageData: cloneDeep(baseData),
      form1Ref: {},
      form2Ref: {}
    })

    const store = getStoreFromInstance()

    const user = computed(() => store.getters.user)

    function initData() {
      if (props.data.jsonText && props.data.jsonText !== '{}') {
        const jsonText = JSON.parse(props.data.jsonText)
        state.pageData = Object.assign(cloneDeep(baseData), jsonText)
      } else {
        nextTick(() => {
          const pageData = cloneDeep(baseData)
          state.pageData = pageData
        })
      }
    }

    function getParams() {
      return {
        form1: state.form1Ref.getParams(),
        form2: state.form2Ref.getParams()
      }
    }

    watch(
      () => props.data,
      () => {
        initData()
      }
    )

    onMounted(() => {
      initData()
    })

    return {
      ...toRefs(state),
      getParams
    }
  }
})
</script>

<style lang="scss" scoped>
.container1 {
  // mac 下需写成像素单位，否则打印预览时空白
  height: 1188px;
  padding: 20px 0;
  line-height: 23px;
  font-family: SiYuanSong-Medium;
  font-size: 13px;
  text-align: start;
}
</style>
